<?php

class News extends AppModel {

    var $alias = 'News';
    var $name = 'News';
    var $useTable = 'news';
    var $belongsTo = array(
        'Catnews' => array(
            'className' => 'Catnews',
            'foreignKey' => 'cat_id',
            'fields' => array('Catnews.id', 'Catnews.title'),
        )
    );

    function delete($id = null, $cascade = true) {
        App::import('Component', 'Upload');
        $this->id = $id;
        $upload = new UploadComponent();

        //delete thumbnail (132x99)
        $thumbnail = $this->field('thumbnail');
        if (is_file(Configure::read('news_image_absolute_path') . $thumbnail) && $thumbnail != 'no_image.gif') {
            $upload->delete(Configure::read('news_image_absolute_path') . $thumbnail);
        }
        //delete thumbnail (88x58)
        $thumbnail = $this->field('thumbnail_small');
        if (is_file(Configure::read('news_image_absolute_path') . $thumbnail) && $thumbnail != 'no_image.gif') {
            $upload->delete(Configure::read('news_image_absolute_path') . $thumbnail);
        }
        //delete slideshow (365x280)
        
        $thumbnail = $this->field('thumbnail_slideshow');
        if (is_file(Configure::read('news_image_absolute_path') . $thumbnail) && $thumbnail != 'no_image.gif') {
            $upload->delete(Configure::read('news_image_absolute_path') . $thumbnail);
        }
        
        parent::delete($id, $cascade);
    }

    function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
        $parameters = compact('conditions');
        $this->recursive = $recursive;
        $count = $this->find('count', array_merge($parameters, $extra));
        if (isset($extra['group'])) {
            $count = $this->getAffectedRows();
        }
        return $count;
    }

    function cacheLastestNews($limit = 10) {

        $cacheName = 'lastest_news';
        $data = $this->find('all', array(
                    'conditions' => array('News.published' => '1'),
                    'fields' => array('News.id', 'News.alias', 'News.title'),
                    'order' => 'News.id DESC',
                    'limit' => $limit,
                ));
        Cache::write($cacheName, $data, 'news');

        return $data;
    }
    function writeHomeLastestNews($limit = 4){
        $data = $this->find('all', array(
                    'conditions' => array('News.published' => '1', 'News.special_news' => '1'),
                    'fields' => array('News.short_content','News.id', 'News.alias', 'News.title', 'News.thumbnail_small', 'News.thumbnail_slideshow'),
                    'order' => 'News.id DESC',
                    'limit' => $limit,
        ));
        $default_news   =   current($data);
        
        $html   =   '<div id="slide-container">' . "\n";
        $html   .= '<div id="slide"><?php echo $this->Html->image(\'' . Configure::read('news_image_url_path') . $default_news['News']['thumbnail_slideshow'] . '\', array(\'alt\'=>\'slider1\', \'width\'=> \'365px\', \'height\'=> \'280px\')); ?>
            <div class="slide-text">
              <div class="slidetext">
                <h3><a href="/tin-tuc/chi-tiet/' . $default_news['News']['id'] . '-' . $default_news['News']['alias'] . '.html"> ' .$default_news['News']['title'] . '</a></h3>
              </div>
            </div>
          </div>'. "\n";
          $html   .= '<div id="slide-navigation">
            <div id="nav-slide">
              <ul>'. "\n";
          foreach($data AS  $row){
          $html   .=  '<li class="first"> <a href="/tin-tuc/chi-tiet/' . $row['News']['id'] . '-' . $row['News']['alias'] . '.html"><?php echo $this->Html->image(\'' . Configure::read('news_image_url_path') . $row['News']['thumbnail_small'] . '\',array("width"=>"88px", "height"=>"58px")); ?></a>
                  <input type="hidden" name="thumb_lastnews" class="thumb_lastnews" value="' . Configure::read('news_image_url_path') . $row['News']['thumbnail_slideshow'] . '" />
                  <h3><a href="/tin-tuc/chi-tiet/' . $row['News']['id'] . '-' . $row['News']['alias'] . '.html">' . $row['News']['title'] . '</a></h3>
                  <p>' . mb_substr($row['News']['short_content'], 0, min(60, mb_strlen($row['News']['short_content']))) . '...</p>
                   </li>';
          }
          $html   .=  '
              </ul>
            </div>
          </div>'. "\n";
        $html   .=   '</div>';
        $resourse   =   fopen(CAKE_CORE_INCLUDE_PATH . DS . 'app' . DS . 'views' . DS . 'elements' . DS . 'home_lastest_news.ctp', 'w');
        fwrite($resourse, $html);
    }
    function writeHomeFashionNews($cat_id = 2, $limit = 6){
        $data = $this->find('all', array(
                    'conditions' => array('News.published' => '1', 'News.cat_id'=>$cat_id),
                    'fields' => array('News.short_content','News.id', 'News.alias', 'News.title', 'News.thumbnail_small', 'News.thumbnail_slideshow'),
                    'order' => 'News.id DESC',
                    'limit' => $limit,
        ));
        
        $html   =   '<ul class="dlbd">' . "\n";
          foreach($data AS  $row){
          $html   .=  '<li><a href="/tin-tuc/chi-tiet/' . $row['News']['id'] . '-' . $row['News']['alias'] . '.html">' . $row['News']['title'] . '</a></li>';
          }
          $html   .=  '</ul>';
        $resourse   =   fopen(CAKE_CORE_INCLUDE_PATH . DS . 'app' . DS . 'views' . DS . 'elements' . DS . 'home_fashion_news.ctp', 'w');
        fwrite($resourse, $html);
    }
    function writeHomeHotNews($limit = 6){
        $data = $this->find('all', array(
                    'conditions' => array('News.published' => '1', 'News.hot_news'=> '1'),
                    'fields' => array('News.id', 'News.alias', 'News.title', 'News.thumbnail', 'News.thumbnail_slideshow'),
                    'order' => 'News.id DESC',
                    'limit' => $limit,
        ));

        $html   =   '<div id="tintucnoibat"><div class="tintucnoibat1">
          <h3>TIN TỨC NỔI BẬT</h3>
        </div>' . "\n";
          foreach($data AS  $row){
            $html   .=  '<ul class="tintucnoibat2"><li><a href="/tin-tuc/chi-tiet/' . 
              $row['News']['id'] . '-' . $row['News']['alias'] . '.html">
                  <img src=" ' . Configure::read('news_image_url_path') . $row['News']['thumbnail'] .
              '" /></a></li><li><a href="/tin-tuc/chi-tiet/' .
              $row['News']['id'] . '-' . $row['News']['alias'] . '.html">' . $row['News']['title'] . '</a></li></ul>';
          }
          $html   .=  '<p>
            <a href="/tin-tuc.html">Xem thêm</a>
            </p></div>';
        $resourse   =   fopen(CAKE_CORE_INCLUDE_PATH . DS . 'app' . DS . 'views' . DS . 'elements' . DS . 'home_hotnews.ctp', 'w');
        fwrite($resourse, $html);
    }
    function writeHomeNotice(){
        $data = $this->find('all', array(
                    'conditions' => array('News.published' => '1', 'News.cat_id'=> Configure::read('news_promotion_id')),
                    'fields' => array('News.id', 'News.alias', 'News.title'),
                    'order' => 'News.id DESC'
        ));

        $html   =   '<ul class="lastest-notice">' . "\n";
        foreach($data AS  $row){
            $html   .=  '<li>' .$row['News']['title'] . ' <a href="/tin-tuc/chi-tiet/' .
              $row['News']['id'] . '-' . $row['News']['alias'] . '.html">Xem tại đây</a></li>';
        }
        $html   .=  '</ul>';
        $resourse   =   fopen(CAKE_CORE_INCLUDE_PATH . DS . 'app' . DS . 'views' . DS . 'elements' . DS . 'home_notices.ctp', 'w');
        fwrite($resourse, $html);
    }
}

?>
